﻿Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml.Linq
Imports Exp.Model
Imports Exp.DATA_Service
Imports Exp.DATA_Service.Exp.DATA_Service

Public Class Brand
    Inherits System.Web.UI.Page
    Const MENUNAME As String = "BRAND"
    Const SaveSuccess As String = "Save completed."
    Const SaveError As String = "Save error !"
    Const DeleteSuccess As String = "Delete completed."
    Const DeleteError As String = "Delete not complete"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Session("USER") Is Nothing Then
                Response.Redirect("~/login.aspx")
            Else
                Dim objUser As REM_User = CType(Session("USER"), REM_User)

                Dim objUSV As New Exp.DATA_Service.User_Service
                If Not objUSV.isHasRight(objUser.User_ID, MENUNAME) Then
                    Session("USER") = Nothing
                    Session.Clear()
                    Response.Redirect("~/login.aspx")
                End If

            End If
            BindData()
            BindStatus()
        End If

    End Sub
#Region "Fucntion"
    Sub Save()
        Dim objREM_Brand As New REM_Brand
        Dim objUser As REM_User = CType(Session("USER"), REM_User)
        If txtBrandName.Text <> "" Then
            If CheckNameExisting(txtBrandName.Text, 0) Then
                JavaScript.AlertMessage(Me.Page, "Data existing, please try again")
                Return
            End If
            Dim Brand_service As New Brand_Service
            'Dim iBrandID As Integer = Brand_service.GetLastID()
            'objREM_Brand.Brand_ID = iBrandID
            objREM_Brand.BrandName = txtBrandName.Text
            objREM_Brand.isActive = ddl_Status.SelectedValue
            objREM_Brand.UpdateDate = CType(Now, Date)
            objREM_Brand.CreateDate = CType(Now, Date)
            objREM_Brand.UpdateUser = objUser.User_ID
            objREM_Brand.CreateUser = objUser.User_ID
            Brand_service.Add(objREM_Brand)
            JavaScript.AlertMessage(Me.Page, "Save data complete")
        Else
            JavaScript.AlertMessage(Me.Page, "Please insert data, try again")
        End If
    End Sub
    Sub Edit()
        Dim objUser As REM_User = CType(Session("USER"), REM_User)
        Dim objREM_Brand As New REM_Brand
        If txtBrandName.Text <> "" Then
            If CheckNameExisting(txtBrandName.Text, HDFieldBrandID.Value) Then
                JavaScript.AlertMessage(Me.Page, "Data existing, please try again")
                Return
            End If
            Dim Brand_Service As New Brand_Service
            objREM_Brand.Brand_ID = HDFieldBrandID.Value
            objREM_Brand.BrandName = txtBrandName.Text
            objREM_Brand.isActive = ddl_Status.SelectedValue
            objREM_Brand.UpdateDate = CType(Now, Date)
            objREM_Brand.UpdateUser = objUser.User_ID
            Brand_Service.Edit(objREM_Brand)
            HDFieldBrandID.Value = ""
            JavaScript.AlertMessage(Me.Page, "Save Data Complete")
        Else
            JavaScript.AlertMessage(Me.Page, "Please insert data, try again")
        End If
    End Sub
    Sub BindData()
        Dim Brand_service As New Brand_Service
        Dim objREM_Brand As New List(Of REM_Brand)
        objREM_Brand = Brand_service.GetAll()
        gv_Data.DataSource = objREM_Brand
        gv_Data.DataBind()
    End Sub
    Sub BindStatus()
        'ddl_Status.Items.Clear()
        'ddl_Status.Items.Add(0, "Active")
        'ddl_Status.Items.Add("InActive")
        ddl_Status.SelectedValue = ddl_Status.Items.FindByText("Active").Value
    End Sub

    Function CheckNameExisting(ByVal sName As String, ByVal ID As Integer) As Boolean
        Dim Brand_Service As New Brand_Service
        Dim LobjREM_Brand As New List(Of REM_Brand)

        LobjREM_Brand = Brand_Service.GetByName(sName)
        If LobjREM_Brand Is Nothing Then
            Return False
        Else
            If LobjREM_Brand.Count > 0 Then
                If LobjREM_Brand.Item(0).Brand_ID = ID Then
                    Return False
                End If
                Return True
            End If
        End If
        Return False
    End Function
#End Region
#Region "Event"
    Protected Sub btn_Save_Click(sender As Object, e As EventArgs) Handles btn_Save.Click
        If HDFieldBrandID.Value = "" Then
            Save()
            txtBrandName.Text = ""
            MultiView1.ActiveViewIndex = 0
            BindData()
        Else
            Edit()
            txtBrandName.Text = ""
            MultiView1.ActiveViewIndex = 0
            BindData()
        End If

    End Sub
    Private Sub gv_data_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gv_data.RowCommand
        Dim commandArgs As String = e.CommandArgument.ToString()
        Dim REM_Brand As New REM_Brand
        Dim Brand_Service As New Brand_Service
        Dim iBrandID As Integer
        iBrandID = CType(commandArgs, Integer)
        If e.CommandName.ToLower = "edit" Then
            REM_Brand = Brand_Service.GetByID(iBrandID)

            If Not REM_Brand Is Nothing Then
                If REM_Brand.BrandName <> "" Then
                    txtBrandName.Text = REM_Brand.BrandName
                    If REM_Brand.isActive Then
                        ddl_Status.SelectedValue = 1
                    Else
                        ddl_Status.SelectedValue = 0
                    End If
                    HDFieldBrandID.Value = iBrandID
                    MultiView1.ActiveViewIndex = 1
                Else
                    txtBrandName.Text = ""
                End If
            End If

        ElseIf e.CommandName.ToLower = "delete" Then
            Brand_Service.Delete(iBrandID)
            txtBrandName.Text = ""
            ddl_Status.SelectedValue = 1
        End If
    End Sub
    Private Sub gv_data_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv_data.RowDataBound
        Try

            If e.Row.RowType = DataControlRowType.DataRow Then

                e.Row.Cells(3).Text = CType(e.Row.Cells(3).FindControl("lblBrandName"), Label).Text
                ''CType(e.Row.Cells(18).FindControl("chk_IMMEDIATE"), CheckBox)
                e.Row.Cells(5).Text = Format(CType(CType(e.Row.Cells(5).FindControl("lblCreateDate"), Label).Text, Date), "dd/MM/yyyy")
                e.Row.Cells(6).Text = Format(CType(CType(e.Row.Cells(6).FindControl("lblUpdateDate"), Label).Text, Date), "dd/MM/yyyy")
                GetDataUtil.CDateFormat(e.Row.Cells(5).Text)
                GetDataUtil.CDateFormat(e.Row.Cells(6).Text)
                Dim User_Service As New User_Service
                Dim LEXP_Select_REM_User_Result As New List(Of EXP_Select_REM_User_Result)
                Dim iUserID As Integer = CType(e.Row.Cells(7).FindControl("lblUpdateUser"), Label).Text
                LEXP_Select_REM_User_Result = User_Service.GetAll.Where(Function(p) p.User_ID = iUserID).ToList

                e.Row.Cells(7).Text = LEXP_Select_REM_User_Result.Item(0).FullName
                e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#99CCFF'")

                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;")

            End If

        Catch ex As Exception

            ' Page.ClientScript.AlertMessage(Me, "gv_Data_RowDataBound", "gv_Data_RowDataBound >>" & ex.Message)

        End Try

    End Sub
    Private Sub gv_data_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles gv_data.RowDeleting
        BindData()
    End Sub
    Private Sub gv_data_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles gv_data.RowEditing
        BindData()
    End Sub
    Private Sub btn_Cancel_Click(sender As Object, e As EventArgs) Handles btn_Cancel.Click
        HDFieldBrandID.Value = ""
        BindData()
    End Sub
    Private Sub btn_Add_Click(sender As Object, e As EventArgs) Handles btn_Add.Click
        HDFieldBrandID.Value = ""
        Me.txtBrandName.Text = ""
        Me.ddl_Status.SelectedIndex = 0
    End Sub
#End Region


End Class